/*
 * Created on 2005-6-8
 *
 */
package com.yl.cap.analyst.user.dao;

import jaoso.framework.dao.hibernate.BaseDAOImpl;

import java.util.List;

import com.yl.cap.analyst.user.domain.Group;


/**
 * @author ZhangQi
 *  
 */
public class UserDAOImpl extends BaseDAOImpl implements UserDAO
{
    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.dao.GroupDAO#getAllGroup()
     */
    public List getAllGroup()
    {
        String hql = "from Group";
        return this.getHibernateTemplate().find(hql);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yl.cap.analyst.user.dao.ViewerDAO#getAllViewer()
     */
    public List getAllUser()
    {
        String hql = "from User";
        return this.getHibernateTemplate().find(hql);
    }

    /* (non-Javadoc)
     * @see com.yl.cap.analyst.user.dao.UserDAO#isGroupExist(java.lang.String)
     */
    public boolean isGroupExist(String name)
    {
        String hql = "from Group g where g.name=?";
        List list = this.getHibernateTemplate().find(hql,name);
        if (list == null || list.size() == 0)
        {
            return false;
        }
        return true;
    }

    /* (non-Javadoc)
     * @see com.yl.cap.analyst.user.dao.UserDAO#isUserExist(java.lang.String)
     */
    public boolean isUserExist(String name)
    {
        String hql = "from Account a where a.name=?";
        List list = this.getHibernateTemplate().find(hql,name);
        if (list == null || list.size() == 0)
        {
            return false;
        }
        return true;
    }

    public Group getGroupByName(String name)
    {
        String hql = "from Group g where g.name=?";
        List list = this.getHibernateTemplate().find(hql,name);
        if (list != null && list.size()!=0)
        {
            return (Group)list.get(0);
        }
        return null;
    }

    public boolean vertifyGroup(String groupId,String userId)
    {
        String hql = "select elements(g1.reportLevel) from Group g1 left join g1.reportLevel gl where g1.id=? and gl in (select elements(g2.reportLevel) from Group g2 left join g2.users g2u where g2u.id=?)";
        Object[] values = {groupId,userId};
        List rlList = this.getHibernateTemplate().find(hql,values);
        return rlList.size()==0;
    }
}